// 一些工具函数

import { ICommandFn } from './interface/bot.interface'
import { IEpidemicInfo } from './request/other'

/**
 * 格式化api介绍
 * @param format api格式
 * @param args api参数
 * @param intro api介绍
 */
export const format = function (format: string, args: string[], intro?: string) {
    return `${format}\n${args.join('\n')}\n${intro || ''}`
}

/**
 * 判断字符串是否符合shard格式
 * @param shard string
 */
export const isShard = function (shard: string) {
    return /^shard[0-3]$/.test(shard)
}

/**
 * 包装函数和help信息
 */
export const wrapCommandFn = function (fn: ICommandFn, help?: () => string) {
    if (help) fn.help = help
    return fn
}

/**
 * 分行输出
 * @param strs
 */
export const splitLine = function (strs: string[]) {
    return strs.join('\n')
}

/**
 * 查询指定地区的疫情信息
 * @param areaTree 地区数
 * @param queryArea 查询地区
 * @returns
 */
export const findArea = (areaTree: IEpidemicInfo[], queryArea: string): IEpidemicInfo | null => {
    for (const area of areaTree) {
        if (area.name === queryArea) {
            return area
        }
        if (area.children) {
            const result = findArea(area.children, queryArea)
            if (result) {
                return result
            }
        }
    }
    return null
}
